#include "ppdef.h"      

      subroutine ssvd_lancz(
#ifdef PARALLEL_ARPACK
     &    comm,
#endif
     &    A, m, n, maxm, maxn, nev, ncv, maxnev, maxncv, ldv, ldu,
     &    w, v, u, workl, workd, s, resid, select, tol,IT,MAKENIT,mytol)
       use ufileformat
       implicit none
       
#ifdef PARALLEL_ARPACK
       include "mpif.h"
#endif

c---------------------------------------------------------------------c
c     "iterativeEOF", Version 2.0, December, 2002.                    c
c      A Krylov-based code for computing EOF                          c
c      Vincent TOUMAZOU                                               c
c      Vincent.Toumazou@cnes.fr                                       c
c      CNES/MERCATOR-Ocean                                            c
c      18, Avenue Edouard Belin                                       c
c      F-31401 TOULOUSE Cedex 4                                       c
c      URL: http://www.mercator.com.fr                                c
c      URL: http://www.cnes.fr                                        c
c---------------------------------------------------------------------c
c     This subroutine computes NEV singular values for statistical    c
c     study of phenomenom. Left and right associated singular vectors c
c     are also computed.                                              c
c                                                                     c
c********************** CAUTION **************************************c
c                                                                     c
c     The computation of the singular elements is performed using     c
c     the freeware ARPACK. Please respect the copyright of ARPACK     c
c     and read the documentation(s) provided with the EOF code before c
c     any use.                                                        c
c                                                                     c
c********************** END OF CAUTION *******************************c
c                                                                     c
c     The forthcoming lines of code are an adaptation of the "Example c
c     Test Program" called "dsvd" from the ARPACK Freeware.           c
c     In order to tune this program (and to transform it into a       c
c     subroutine) some modifications have been done both in the       c
c     comments and in the statements and/or declarations.             c
c                                                                     c
c---------------------------------------------------------------------c
c
c     This subroutine calls ARPACK to find a few of the
c     largest singular values(sigma) and corresponding right singular
c     vectors (v) for the the matrix A by solving the symmetric problem:
c
c                        (A'*A)*v = sigma*v
c 
c     where A is an m by n real matrix.
c
c     This formulation is appropriate when  m  .ge.  n.
c     Reverse the roles of A and A' in the case that  m .le. n.
c
c     The only thing that must be supplied in order to use this
c     routine on your problem is to change the array dimensions 
c     appropriately, to specify WHICH singular values you want to 
c     compute and to supply a the matrix-vector products 
c
c                         w <-  Ax
c                         y <-  A'w
c
c     Further documentation is available in the header of DSAUPD
c     which may be found in the SRC directory.
c
c Routines called:
c ----------------
c     ssaupd  ARPACK reverse communication interface routine.
c     sseupd  ARPACK routine that returns Ritz values and (optionally)
c             Ritz vectors.
c     snrm2   Level 1 BLAS that computes the norm of a vector.
c     saxpy   Level 1 BLAS that computes y <- alpha*x+y.
c     sscal   Level 1 BLAS thst computes x <- x*alpha.
c     scopy   Level 1 BLAS thst computes y <- x.
c
c Authors of the ARPACK kernel:
c -----------------------------
c     Richard Lehoucq
c     Danny Sorensen
c     Chao Yang
c     Dept. of Computational &
c     Applied Mathematics
c     Rice University
c     Houston, Texas
c     For more informations about ARPACK (Use, Updates, Copyright), please
c     read first the documentation provided with the EOF code.
c
c-----------------------------------------------------------------------
c
c     %------------------------------------------------------%
c     | Storage Declarations:                                |
c     |                                                      |
c     | It is assumed that A is M by N with M .ge. N.        |
c     |                                                      |
c     | The maximum dimensions for all arrays are            |
c     | set here to accommodate a problem size of            |
c     | M .le. MAXM  and  N .le. MAXN                        |
c     |                                                      |
c     | The NEV right singular vectors will be computed in   |
c     | the N by NCV array V.                                |
c     |                                                      |
c     | The NEV left singular vectors will be computed in    |
c     | the M by NEV array U.                                |
c     |                                                      |
c     | NEV is the number of singular values requested.      |
c     |     See specifications for ARPACK usage below.       |
c     |                                                      |
c     | NCV is the largest number of basis vectors that will |
c     |     be used in the Implicitly Restarted Arnoldi      |
c     |     Process.  Work per major iteration is            |
c     |     proportional to N*NCV*NCV.                       |
c     |                                                      |
c     | You must set:                                        |
c     |                                                      |
c     | MAXM:   Maximum number of rows of the A allowed.     |
c     | MAXN:   Maximum number of columns of the A allowed.  |
c     | MAXNEV: Maximum NEV allowed                          |
c     | MAXNCV: Maximum NCV allowed                          |
c     %------------------------------------------------------%
c
      integer      maxm, maxn, maxnev, maxncv, ldv, ldu,IT,MAKENIT
c
c     %--------------%
c     | Local Arrays |
c     %--------------%
c
      Real
     &             A(maxm,maxn), w(ldu), v(ldv,maxncv),u(ldu,maxnev), 
     &             workl(maxncv*(maxncv+8)), workd(3*maxn), 
     &             s(maxncv,2), resid(maxn),frigo,vloc(ldv,maxncv),
     &             xloc(maxn),x(maxn)
      Real,allocatable :: B(:,:)
      logical      select(maxncv)
      integer      iparam(11), ipntr(11)
c
c     %---------------%
c     | Local Scalars |
c     %---------------%
c
      character    bmat*1, which*2
      integer      ido, m, n, nev, ncv, lworkl, info, ierr,
     &             j, ishfts, maxitr, mode1, nconv, i,nloc
      logical      rvec
      Real      
     &             tol, sigma, temp, mytol, newtol
c
c     %------------%
c     | Parameters |
c     %------------%
c
      Real
     &             one, zero,valexc
      parameter    (one = 1.0E+0, zero = 0.0E+0, valexc = 9999.)
      
c  
c     %-----------------------------%
c     | BLAS & LAPACK routines used |
c     %-----------------------------%
c
      Real           
     &             snrm2
      external     snrm2, saxpy, scopy, sscal, sgemv

#ifdef PARALLEL_ARPACK
      integer              :: comm,myrank,nbprocs
      integer,allocatable  :: recvcounts(:),displs(:)
#else
      integer,parameter    :: myrank=0     
#endif

c
c     %-----------------------%
c     | Executable Statements |
c     %-----------------------%
c
c     %-------------------------------------------------%
c     | The following include statement and assignments |
c     | initiate trace output from the internal         |
c     | actions of ARPACK.  See debug.doc in the        |
c     | DOCUMENTS directory for usage.  Initially, the  |
c     | most useful information will be a breakdown of  |
c     | time spent in the various stages of computation |
c     | given by setting msaupd = 1.                    |
c     %-------------------------------------------------%
c


      include 'includes/debug.h'
      ndigit = -3
      logfil = 6
      msgets = 0
      msaitr = 0 
      msapps = 0
      msaupd = 0
      msaup2 = 0
      mseigt = 0
      mseupd = 0
c
      bmat  = 'I'
      which = 'LA'
c
      if ( n .gt. maxn ) then
         print *, ' ERROR with _SVD: N is greater than MAXN '
         go to 9000
      else if ( m .gt. maxm ) then
         print *, ' ERROR with _SVD: M is greater than MAXM '
         go to 9000
      else if ( nev .gt. maxnev ) then
         print *, ' ERROR with _SVD: NEV is greater than MAXNEV '
         go to 9000
      else if ( ncv .gt. maxncv ) then
         print *, ' ERROR with _SVD: NCV is greater than MAXNCV '
         go to 9000
      end if

c      ----------------------------------------
c      Initiate MPI sequence
c      ----------------------------------------  

#ifdef PARALLEL_ARPACK
 
      CALL MPI_COMM_RANK( comm, myrank, ierr )
      CALL MPI_COMM_SIZE( comm, nbprocs, ierr )
 
      
      nloc = n / nbprocs
      
      if ( mod(n, nbprocs) .gt. myrank ) nloc = nloc + 1
      allocate(recvcounts(nbprocs),displs(nbprocs))
      do i=1,nbprocs
        recvcounts(i)= n / nbprocs
        if ( mod(n, nbprocs) .gt. i-1 ) recvcounts(i)=recvcounts(i)+1
      enddo
      displs(1)=0
      do i=2,nbprocs
         displs(i)=displs(i-1)+recvcounts(i-1)
      enddo
      
      allocate(B(nloc,n))
      B = matmul( 
     &      transpose(A(:,displs(myrank+1)+1:displs(myrank+1)+nloc)), 
     &                A)
#else

      nloc=n
      allocate(B(n,n))
      B = matmul(transpose(A),A)

#endif

c
c     %-----------------------------------------------------%
c     | Specification of stopping rules and initial         |
c     | conditions before calling SSAUPD                    |
c     |                                                     |
c     |           abs(sigmaC - sigmaT) < TOL*abs(sigmaC)    |
c     |               computed   true                       |
c     |                                                     |
c     |      If TOL .le. 0,  then TOL <- macheps            |
c     |              (machine precision) is used.           |
c     |                                                     |
c     | IDO  is the REVERSE COMMUNICATION parameter         |
c     |      used to specify actions to be taken on return  |
c     |      from SSAUPD. (See usage below.)                |
c     |                                                     |
c     |      It MUST initially be set to 0 before the first |
c     |      call to SSAUPD.                                | 
c     |                                                     |
c     | INFO on entry specifies starting vector information |
c     |      and on return indicates error codes            |
c     |                                                     |
c     |      Initially, setting INFO=0 indicates that a     | 
c     |      random starting vector is requested to         |
c     |      start the ARNOLDI iteration.  Setting INFO to  |
c     |      a nonzero value on the initial call is used    |
c     |      if you want to specify your own starting       |
c     |      vector (This vector must be placed in RESID.)  | 
c     |                                                     |
c     | The work array WORKL is used in SSAUPD as           | 
c     | workspace.  Its dimension LWORKL is set as          |
c     | illustrated below.                                  |
c     %-----------------------------------------------------%
c
      lworkl = ncv*(ncv+8)
      info = 0
      ido = 0
c
c     %---------------------------------------------------%
c     | Specification of Algorithm Mode:                  |
c     |                                                   |
c     | This program uses the exact shift strategy        |
c     | (indicated by setting IPARAM(1) = 1.)             |
c     | IPARAM(3) specifies the maximum number of Arnoldi |
c     | iterations allowed.  Mode 1 of SSAUPD is used     |
c     | (IPARAM(7) = 1). All these options can be changed |
c     | by the user. For details see the documentation in |
c     | SSAUPD.                                           |
c     %---------------------------------------------------%
c
      ishfts = 1
      maxitr = n

c$$$      if(MAKENIT.eq.0) then
c$$$         if(nev**2.le.n) then
c$$$            maxitr = nev**2
c$$$         else 
c$$$            maxitr=n
c$$$         end if
c$$$      elseif(MAKENIT.eq.1) then
c$$$         maxitr = n
c$$$      end if

      mode1 = 1
c
      iparam(1) = ishfts
c                
      iparam(3) = maxitr
c                  
      iparam(7) = mode1
c
c     %------------------------------------------------%
c     | M A I N   L O O P (Reverse communication loop) |
c     %------------------------------------------------%
c

c  nev era 1
       if(IT.EQ.1) then
        do i=1,ldv
         resid(i)=v(i,1)
c          resid(i)=0
c         write(*,*)'aqui',v(i,1)
        end do
        info=1
        
       end if
       


 10   continue
c
c        %---------------------------------------------%
c        | Repeatedly call the routine SSAUPD and take | 
c        | actions indicated by parameter IDO until    |
c        | either convergence is indicated or maxitr   |
c        | has been exceeded.                          |
c        %---------------------------------------------%
c
          
      newtol=max(tol,mytol)
         call ssaupd ( 
#ifdef PARALLEL_ARPACK
     &                 comm,
#endif
     &                 ido, bmat, nloc, which, nev, newtol, resid, 
     &                 ncv, vloc, ldv, iparam, ipntr, workd, workl,
     &                 lworkl, info )
c
         if(IT.EQ.1) then
           !IT=0 ?????
           IT=0
           info=0
         end if

         if (ido .eq. -1 .or. ido .eq. 1) then
c
c           %---------------------------------------%
c           | Perform matrix vector multiplications |
c           |              w <--- A*x       (av())  |
c           |              y <--- A'*w      (atv()) |
c           | The user should supply his/her own    |
c           | matrix vector multiplication routines |
c           | here that takes workd(ipntr(1)) as    |
c           | the input, and returns the result in  |
c           | workd(ipntr(2)).                      |
c           %---------------------------------------%
c
          
#ifdef PARALLEL_ARPACK      
          xloc=workd(ipntr(1):ipntr(1)+nloc-1)
            call mpi_allgatherv(xloc,nloc,MPI_REAL,x,
     &        recvcounts,displs,MPI_REAL,comm,ierr)

          
#else
          x=workd(ipntr(1):ipntr(1)+n-1)
#endif

          if (MAKENIT .eq. 0) then
            call sgemv('N',m,n,one,A,maxm,workd(ipntr(1)),1,zero,
     &                w,1)
            call sgemv('T',m,n,one,A,maxm,w,1,zero,
     &                workd(ipntr(2)),1)
          
          else
            
           
#ifdef PARALLEL_ARPACK
            x(displs(myrank+1)+1:displs(myrank+1)+nloc) = matmul(B,x)
            workd(ipntr(2):ipntr(2)+nloc-1) = 
     &          x(displs(myrank+1)+1:displs(myrank+1)+nloc);
#else
            
            workd(ipntr(2):ipntr(2)+n-1) = matmul(B,x)
#endif

         
          end if     
c
c           %-----------------------------------------%
c           | L O O P   B A C K to call SSAUPD again. |
c           %-----------------------------------------%
c
            go to 10
c
         end if 
c
c     %----------------------------------------%
c     | Either we have convergence or there is |
c     | an error.                              |
c     %----------------------------------------%
c
      if ( info .lt. 0 ) then
c
c        %--------------------------%
c        | Error message. Check the |
c        | documentation in SSAUPD. |
c        %--------------------------%
c
         print *, ' '
         print *, ' Error with _saupd, info = ', info
         print *, ' Check documentation in _saupd '
         print *, ' '
c
      else 
c
c        %--------------------------------------------%
c        | No fatal errors occurred.                  |
c        | Post-Process using SSEUPD.                 |
c        |                                            |
c        | Computed singular values may be extracted. |  
c        |                                            |
c        | Singular vectors may also be computed now  |
c        | if desired.  (indicated by rvec = .true.)  | 
c        |                                            |
c        | The routine SSEUPD now called to do this   |
c        | post processing                            | 
c        %--------------------------------------------%
c           
         rvec = .true.
c
         call sseupd ( 
#ifdef PARALLEL_ARPACK
     &        comm,
#endif
     &        rvec, 'All', select, s, vloc, ldv, sigma, 
     &        bmat, nloc, which, nev, newtol, resid, ncv, vloc, ldv, 
     &        iparam, ipntr, workd, workl, lworkl, ierr )
        
         
c#ifdef PARALLEL_ARPACK
c         if(myrank.eq.0) then
c         call usave('workd_0.dat',workd,valexc)
c         call usave('AtA_0.dat',matmul(transpose(A),A),valexc)
c         write(*,*)'rank0',myrank,s(1:10,1)
c         else
c         write(*,*)'rank1',myrank,s(1:10,1)
c         call usave('AtA_1.dat',matmul(transpose(A),A),valexc)
c         call usave('workd_1.dat',workd,valexc)
c         endif
c#endif
         
          

#ifdef PARALLEL_ARPACK 
         do i=1,maxncv !FIXMEEEEEEEEEEEETOOOOOOOOOOOOOOOOO!!!!!
            call mpi_allgatherv(vloc(:,i),nloc,MPI_REAL,v(:,i),
     &        recvcounts,displs,MPI_REAL,comm,ierr)
         enddo



c         if(myrank.eq.0) then
c         call usave('vtot_0.dat',v,valexc)
c         write(*,*)'rank0',myrank
c         else
c         write(*,*)'rank1',myrank
c         call usave('vtot_1.dat',v,valexc)
c         endif
         




#else
         v=vloc
#endif
         
c
c        %-----------------------------------------------%
c        | Singular values are returned in the first     |
c        | column of the two dimensional array S         |
c        | and the corresponding right singular vectors  | 
c        | are returned in the first NEV columns of the  |
c        | two dimensional array V as requested here.    |
c        %-----------------------------------------------%
c
         if ( ierr .ne. 0) then
c
c           %------------------------------------%
c           | Error condition:                   |
c           | Check the documentation of SSEUPD. |
c           %------------------------------------%
c
            print *, ' '
            print *, ' Error with _seupd, info = ', ierr
            print *, ' Check the documentation of _seupd. '
            print *, ' '
c
         else
c
            nconv =  iparam(5)
            do 20 j=1, nconv
c
               s(j,1) = sqrt(s(j,1))
c
c              %-----------------------------%
c              | Compute the left singular   |
c              | vectors from the formula    |
c              |                             |
c              |     u = Av/sigma            |
c              |                             |
c              | u should have norm 1 so     |
c              | divide by norm(Av) instead. |
c              %-----------------------------%
c
               call sgemv('N',m,n,one,A,maxm,v(1,j),1,
     &                   zero,u(1,j),1)
               temp = one/s(j,1)
               call sscal(m, temp, u(1,j), 1)
               temp = one/snrm2(m, u(1,j), 1)
               call sscal(m, temp, u(1,j), 1)
               
c
c              %---------------------------%
c              |                           |
c              | Compute the residual norm |
c              |                           |
c              |   ||  A*v - sigma*u ||    |
c              |                           |
c              | for the NCONV accurately  |
c              | computed singular values  |
c              | and vectors.  (iparam(5)  |
c              | indicates how many are    |
c              | accurate to the requested |
c              | tolerance).               |
c              | Store the result in 2nd   |
c              | column of array S.        |
c              %---------------------------%
c
               call scopy(m, u(1,j), 1, w, 1)
               call sgemv('N',m,n,one,A,maxm,v(1,j),1,-s(j,1),w,1)
               s(j,2) = snrm2(m, w, 1)
c
 20         continue
c
c           %-------------------------------%
c           | Display computed residuals    |
c           %-------------------------------%
c
            if (msaupd.gt.0) then
            call smout(6, nconv, 2, s, maxncv, -6,
     &                'Singular values and direct residuals')
            end if
         end if
c
c        %------------------------------------------%
c        | Print additional convergence information |
c        %------------------------------------------%
c
         if ( info .eq. 1) then
            print *, ' '
            print *, ' Maximum number of iterations reached.'
            print *, ' '
         else if ( info .eq. 3) then
            print *, ' ' 
            print *, ' No shifts could be applied during implicit
     &                 Arnoldi update, try increasing NCV.'
            print *, ' '
         end if      

         if (msaupd.gt.0) then 
         print *, ' '
         print *, ' _SVD '
         print *, ' ==== '
         print *, ' '
         print *, ' Size of the matrix is ', n
         print *, ' The number of Ritz values requested is ', nev
         print *, ' The number of Arnoldi vectors generated',
     &            ' (NCV) is ', ncv
         print *, ' What portion of the spectrum: ', which
         print *, ' The number of converged Ritz values is ', 
     &              nconv 
         print *, ' The number of Implicit Arnoldi update',
     &            ' iterations taken is ', iparam(3)
         print *, ' The number of OP*x is ', iparam(9)
         print *, ' The convergence criterion is ', tol, newtol
         print *, ' '
         end if
           

      end if
c
c     %-------------------------%
c     | Done with program ssvd. |
c     %-------------------------%
c
         deallocate(B)
#ifdef PARALLEL_ARPACK 
         deallocate(recvcounts,displs)
#endif

 9000 continue
c v(ldv,maxncv), u(ldu, maxnev) s(maxncv,2)

          do i=1,ldu
           do j=1,nev/2
            frigo=u(i,j)
            u(i,j)=u(i,nev+1-j)
            u(i,nev+1-j)=frigo
           enddo
          enddo

          do i=1,ldv
           do j=1,nev/2
            frigo=v(i,j)
            v(i,j)=v(i,nev+1-j)
            v(i,nev+1-j)=frigo
           enddo
          enddo

          do i=1,nev/2     
           do j = 1,2
            frigo=s(i,j)      
            s(i,j)=s(nev+1-i,j)
            s(nev+1-i,j)=frigo
           enddo
          enddo     
          
      end
